import math, os, random, sys
from properties import Properties


def progress(percent, lines, width=50):
    if percent >= 100:
        percent = 100
    show_str = ('[%%-%ds]' % width) % (int(width * percent / 100) * "#")
    print('\r%s %d%% [%5d]' % (show_str, percent,lines), end='')


def _convert_seq2seq_files(props, questions, answers):
    test_set_size = int(props.getProperties('Attrs.test_set_size'))
    # 创建文件
    train_enc = open(props.getProperties('Path.train.train_enc'), 'w', encoding='utf-8')  # 问
    train_dec = open(props.getProperties('Path.train.train_dec'), 'w', encoding='utf-8')  # 答
    test_enc = open(props.getProperties('Path.test.test_enc'), 'w', encoding='utf-8')  # 问
    test_dec = open(props.getProperties('Path.test.test_dec'), 'w', encoding='utf-8')  # 答

    test_percent = test_set_size / len(questions)
    print('start processing ask-answer data sets')
    for i in range(len(questions)):
        if random.random() < test_percent:
            test_enc.write(questions[i] + '\n')
            test_dec.write(answers[i] + '\n')
        else:
            train_enc.write(questions[i] + '\n')
            train_dec.write(answers[i] + '\n')
        if i % 1000 == 0:
            progress(i*100 / len(questions), i)

    print('\n')
    train_enc.close()
    train_dec.close()
    test_enc.close()
    test_dec.close()


def process_subtitle_data():
    props = Properties('standard.props')
    props.readProperties()
    conv_path  = props.getProperties('Path.subtitle')
    if not (os.path.exists(conv_path)) :
        print('Data file is not exist.', file=sys.stderr)

    with open(conv_path, 'rU', encoding='utf-8') as f:
        ask = []
        response = []
        for line in f:
            encline, decline = line.split("|")
            ask.append(encline)
            response.append(decline)

    _convert_seq2seq_files(props, ask, response)

if __name__ == '__main__':
    process_subtitle_data()